package example.dawangba.com.z09315222_finalandroidhomework;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class PlayVideoActivity extends Activity {

    ListView listView;
    List<String> listName,listPath;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_playvideo);

        listView = (ListView) findViewById(R.id.videoList);
        //指定要读的视频文件存放的路径
        final File path = new File(Environment.getExternalStorageDirectory()+"/Movies");
        listName = new ArrayList<String>();
        listPath = new ArrayList<String>();
        getVideoLists(path);
        ArrayAdapter<String> video_adapter = new ArrayAdapter<String>(PlayVideoActivity.this,
                android.R.layout.simple_list_item_1,listName);
        listView.setAdapter(video_adapter);

        //listView点击事件
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(PlayVideoActivity.this, VideoPlayTest.class);
                //获取当前ListView中被选中的item的text值,其中text1是simple_list_item_1中的textView的id
                TextView name = (TextView) view.findViewById(android.R.id.text1);
                String getName = name.getText().toString();
                //传递选中的视频文件的路径
                intent.putExtra("path",path+"/"+getName);
                startActivity(intent);
            }
        });
    }

    //将视频文件的名字显示在listView中
    private void getVideoLists(File path) {
        if (path != null){
            if (path.isDirectory()){
                File[] files = path.listFiles();
                for (int i = 0; i < files.length; i++) {
                    getVideoLists(files[i]);
                }
            } else {
                String movieName = path.getName();
                String moviePath = path.getAbsolutePath();
                if (movieName.endsWith(".3gp")){
                    listName.add(movieName);
                    listPath.add(moviePath);
                }
            }
        }
    }
}
